package com.sdry.product.prod.controller;
import	java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.basic.entity.Material;
import com.sdry.basic.entity.ProductionStation;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseController;
import com.sdry.common.ResultAppInfo;
import com.sdry.exception.BizException;
import com.sdry.product.prod.entity.LacquerInto;
import com.sdry.product.prod.entity.ProductTwoPaint;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.mapper.LacquerIntoMapper;
import com.sdry.product.prod.mapper.ProductTwoPaintMapper;
import com.sdry.product.prod.service.LacquerIntoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description app端喷涂线
 * @Author chenBin
 * @Date 2020-04-24 15:40
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "lacquerIntoApp")
public class LacquerIntoAppController extends BaseController {

    @Resource
    private LacquerIntoMapper lacquerIntoMapper;

    @Resource
    private ProductTwoPaintMapper productTwoPaintDao;

    /**
     * 服务对象
     */
    @Resource
    private LacquerIntoService lacquerIntoService;

    /**
     * 喷涂线工位绑定
     * @param json 数据
     * @param userId 用户ID
     * [
     *        {
     * 		"psnumber": "20-11",
     * 		"psId": "21",
     *       "newPsnumber":"15-19",
     *       "newPsId":"25",
     * 		"list": [
     *                    {
     * 					"maId": "15",
     * 					"quantity": "298",
     * 					"maSprayingRequirements": "银色",
     * 					"remark04": "WZZ"
     *                    }
     * 				]
     *    }
     * ]
     * @return  ResultAppInfo
     */
    @RequestMapping(value = "/api/bindingLacquerInto")
    public ResultAppInfo bindingLacquerInto(String json, String userId) {
        try {
            logger.info("喷涂线工位绑定1 json = " +json);
            lacquerIntoService.bindingLacquerInto(json,userId);
            return getResultAppInfo("1",null,"");
        }catch (BizException e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, e.getMessage());
        }catch (Exception e1) {
            e1.printStackTrace();
            return getResultAppInfo("0",null, BaseAPIMessageTypeEnum.A001.getName());
        }
    }

    /**
     * 工位绑定
     * @param json
     * @param userId
     * @return
     */
    @RequestMapping(value = "/api/bindingLacquerInto2")
    public ResultAppInfo bindingLacquerInto2(String json, String userId) {
        try {
            logger.info("喷涂线工位绑定2 json = " +json);
            lacquerIntoService.bindingLacquerInto2(json,userId);
            return getResultAppInfo("1",null,"");
        } catch (BizException e) {
            e.printStackTrace();
            return getResultAppInfo("0",null,e.getMessage());
        } catch (Exception e) {
            return getResultAppInfo("0",null,e.getMessage());
        }
    }

    /**
     * 选择工位
     * @param permissionType
     * @return
     */
    @RequestMapping(value = "/api/productionStationList")
    public ResultAppInfo queryWorkstation(String permissionType) {
        List<ProductionStation> productionStationList = lacquerIntoService.queryWorkstation(permissionType);
        return getResultAppInfo("1",productionStationList,"");
    }

    /**
     *根据工位查询喷涂物料
     */
    @RequestMapping(value = "/api/queryPtMateriel")
    public ResultAppInfo queryPtMateriel(String psnumber){
        logger.info("根据工位查询喷涂物料 psnumber = {}" + psnumber);
        try {
            String[] containerNumbers = psnumber.split(",");
            List<ResultAppList<Material>> resultAppListList = lacquerIntoService.queryPtMateriel(containerNumbers);
            return getResultAppInfo("1",resultAppListList,"");
        } catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null,BaseAPIMessageTypeEnum.A001.getName());
        }
    }


    /**
     * 选择喷漆线工位
     * @return
     */
    @RequestMapping(value = "/api/queryWorkstationCode")
    public ResultAppInfo queryWorkstationCode() {
        logger.info("选择喷漆线工位");
        QueryWrapper<ProductTwoPaint> queryWrapper02 = new QueryWrapper<>();
        queryWrapper02 .and(wrapper -> wrapper.eq("remark","0").or().eq("remark", "1"));
        List<ProductTwoPaint> productTwoPaints = productTwoPaintDao.selectList(queryWrapper02);
        Set<String> stringSet = new HashSet<>();
        for (ProductTwoPaint productTwoPaint : productTwoPaints) {
            stringSet.add(productTwoPaint.getOldPsNumber());
        }
        List<ProductionStation> productionStationList = new ArrayList<>();
        Set<ProductionStation> productionStationSet = new HashSet<>();
        QueryWrapper<LacquerInto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0L);
        List<LacquerInto> lacquerIntoList = lacquerIntoMapper.selectList(queryWrapper);
        for (LacquerInto lacquerInto : lacquerIntoList) {
            if (!stringSet.contains(lacquerInto.getStationNumber())){
                ProductionStation productionStation = new ProductionStation();
                productionStation.setPsid(lacquerInto.getStationId());
                productionStation.setPsnumber(lacquerInto.getStationNumber());
                productionStationSet.add(productionStation);
            }
        }
        productionStationList.addAll(productionStationSet);
        return getResultAppInfo("1",productionStationList,"");
    }
}
